package cn.incoder.moyun.service.system;

import cn.incoder.moyun.mapper.system.UserMapper;
import cn.incoder.moyun.pojo.system.User;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mzt.logapi.starter.annotation.LogRecord;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Random;

import static cn.incoder.moyun.service.log.enums.LogRecordConstants.*;

@Service
@Slf4j
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService{
    @Override
    @Transactional
    @LogRecord(type = SYSTEM_USER_TYPE, subType = SYSTEM_USER_CREATE_SUB_TYPE,bizNo ="{{#user.id}}",
    success = SYSTEM_USER_CREATE_SUCCESS, fail = "{{#user.username}}创建失败！")
    public boolean creatUser(User user) {
        Random random = new Random();
        user.setId(Long.valueOf(random.nextInt(1000)));
        user.setCreateTime(LocalDateTime.now());
        user.setUpdateTime(LocalDateTime.now());
        User userIfExist = this.getOne(new QueryWrapper<User>().eq("username", user.getUsername()));
        if (userIfExist != null) {
            log.error("用户名已存在");
            return false;
        }
        return this.save(user);
    }
}
